﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Threading;

namespace TankWin.Classes
{
    class ObjTank
    {
        public Point _position;//vi tri cua tank
        protected EStatusDirect _direct;//huong di cua tank
        protected Image[] _image;//luu hinh tank dang huong theo huong nao
        protected ObjBullet _bullet;//dung cho GUIPlay goi toi ham MoveFoward
        protected EStatusTank _status;//tank con song hay chet
        const int _Move = 10;//do dai moi lan di cua xe tank
        const int _Tank_Size = 40;//kich thuoc 1 o xe tang
        const int _Move_Bullet = 10;
        ///////////////////////////////////
        /* property*/
        #region property
        public Point Position
        {
            get { return _position; }
            set { _position = value; }
        }
        public EStatusDirect Direct
        {
            get { return _direct; }
            set { _direct = value; }

        }
        public Image[] ImageT
        {
            get { return _image; }
            set { _image = value; }
        }
        public ObjBullet Bullet
        {
            get { return _bullet; }
            set { _bullet = value; }

        }
        public EStatusTank Status
        {
            get { return _status; }
            set { _status = value; }
        }
        #endregion

        /*ham khoi tao*/
        public ObjTank(Point _position, EStatusDirect _d, int _c)
        {
            this._position = _position;
            this._direct = _d;

            Point _bulletPosition = new Point();
            switch (this._direct)
            {
                case EStatusDirect.Up:
                    _bulletPosition.X = _position.X + _Tank_Size/2;
                    _bulletPosition.Y = _position.Y+_Move;
                    break;
                case EStatusDirect.Down:
                    _bulletPosition.X = _position.X + _Tank_Size / 2;
                    _bulletPosition.Y = _position.Y + _Tank_Size-_Move;
                    break;
                case EStatusDirect.Left:
                    _bulletPosition.X = _position.X+_Move ;
                    _bulletPosition.Y = _position.Y + _Tank_Size / 2;
                    break;
                case EStatusDirect.Right:
                    _bulletPosition.X = _position.X +_Tank_Size-_Move;
                    _bulletPosition.Y = _position.Y + _Tank_Size / 2;
                    break;

            }
            _bullet = new ObjBullet(_bulletPosition, EStatusDirect.Up);//mac dinh
            _status = EStatusTank.IsAlive;//xe tang con song
            switch (_c)
            {
                case 0:
                    _image = ObjImage._Yellow;//phe ta
                    break;
                    //phe dich
                case 1:
                    _image = ObjImage._Blue;
                    break;
                case 2:
                    _image = ObjImage._Gray;
                    break;
            }
        }

        public void Draw(Graphics _g)
        {
            if (this._status == EStatusTank.IsAlive)
                _g.DrawImageUnscaled(this._image[(int)this._direct], new Rectangle(_position.X, _position.Y, _Tank_Size, _Tank_Size));
        }
        //Di chuyen tank theo huong
        virtual public void MoveFoward()
        {
            //lay toa do cua tank tai thoi diem nay
            //kiem tra huong di neu tank o huong nao thi di huong do
            switch (this._direct)
            {
                case EStatusDirect.Up:
                    TurnUp();
                    break;
                case EStatusDirect.Down:
                    TurnDown();
                    break;
                case EStatusDirect.Left:
                   TurnLeft();
                    break;
                case EStatusDirect.Right:
                    TurnRight();
                    break;

            }

        }
        private void TurnUp()
        {
            this._position.Y -= _Move;
        }
        private void TurnDown()
        {
            this._position.Y += _Move;
        }
        private void TurnLeft()
        {
            this._position.X -= _Move;
        }
        private void TurnRight()
        {
            this._position.X += _Move;
        }
        //ham ban ra vien dan va tra ve vien dan cua no
        public void Fire()
        {
            /////////////////////////////////////////////////
            /*Tinh toan tuy theo toa do cua tank ma thay doi vi tri vien dan,mau gi, huong nao
             * va chu y la set satatus cua vien dan =EStatusTank.IsAlive
             * */
            /////////////////////////////////////////////////
            //tao ra mot vien dan moi voi vi tri,huong hien tai trung voi tank,co mau do

            if (_bullet.Status == EStatusTank.IsAlive) return;

            Point _bulletPosition = new Point();
            switch (this._direct)
            {
                case EStatusDirect.Up:
                    Console.WriteLine("move");
                    _bulletPosition.X = this._position.X + _Tank_Size / 2;
                    _bulletPosition.Y = this._position.Y + _Move_Bullet;
                    break;
                case EStatusDirect.Down:
                    _bulletPosition.X = this._position.X + _Tank_Size / 2;
                    _bulletPosition.Y = this._position.Y + _Tank_Size - _Move_Bullet;
                    break;
                case EStatusDirect.Left:
                    _bulletPosition.X = this._position.X + _Move_Bullet;
                    _bulletPosition.Y = this._position.Y + _Tank_Size / 2;
                    break;
                case EStatusDirect.Right:
                    _bulletPosition.X = this._position.X + _Tank_Size - _Move_Bullet;
                    _bulletPosition.Y = this._position.Y + _Tank_Size / 2;
                    break;
            }
            _bullet.Position = _bulletPosition;
            _bullet.Direct = this._direct;
            _bullet.Status = EStatusTank.IsAlive;
        }
        //tao hieu ung no tung va huy tang
        public void Destroy(Graphics _g)
        {
            //while (this._status == EStatusTank.IsAlive)
            //{
                //Thread t = new Thread(() =>
                //{
                    try
                    {
                        _g.DrawImageUnscaled(global::TankWin.ImageTank.big_burn, _position.X - 15, _position.Y - 15);
                    }
                    catch (Exception ex)
                    {
                    }
            //    });
            //    t.Start();
            //};
        }
    }
   
   
}
